package logic

import (
	"context"
	"errors"
	"video-hub/model/mysql"

	"video-hub/videorpc/internal/svc"
	"video-hub/videorpc/videorpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CollectAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCollectAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CollectAddLogic {
	return &CollectAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CollectAddLogic) CollectAdd(in *videorpc.CollectAddRequest) (*videorpc.CollectAddResponse, error) {
	// todo: add your logic here and delete this line
	collect := mysql.Collect{
		UserId:  int(in.UserId),
		VideoId: int(in.VideoId),
	}
	err := collect.FindUserVideo(mysql.DB, in)
	if err != nil {
		return nil, err
	}
	if collect.ID == 0 {
		collect.CollectStatus = 1
		err = collect.CollectAdd(mysql.DB)
		if err != nil {
			return nil, err
		}
	} else {
		collect.CollectStatus = 0
		return nil, errors.New("已经收藏该视频")
	}

	return &videorpc.CollectAddResponse{
		CollectId: int64(collect.ID),
	}, nil
}
